package com.example.thread.base.demo3;

import lombok.extern.slf4j.Slf4j;


/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/12/14 22:29
 *  volatile修饰词 停止线程，一般情况下可行
 *
 */
@Slf4j
public class Demo3_2 {
    public static void main(String[] args) throws Exception {
        VolatileStopThread volatileStopThread = new VolatileStopThread();
        Thread thread=new Thread(volatileStopThread);
        thread.start();
        Thread.sleep(3000);
        volatileStopThread.cancle=true;
    }
}
class VolatileStopThread implements Runnable{
    public volatile boolean cancle=false;
    @Override
    public void run() {
        try {
            int num=0;
            int numLimit=100000;
            while (!cancle && num<= numLimit){
                if(num %10 ==0){
                    System.out.println("是10的倍数："+num);
                }
                num++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
